use std::{thread, sync::mpsc::{channel, Sender, Receiver}};

use self::{func::Function, keeper::Keeper, receiver::start as start_receiver, finder::start as start_finder};

mod finder;
mod receiver;
mod conf;
mod keeper;
pub mod func;

pub fn start() {
    let mut keeper = Keeper::init();
    let (sx, rx): (Sender<Box<dyn Function>>, Receiver<Box<dyn Function>>) = channel();

    let thread_sx_finder = sx.clone();
    thread::spawn(move || {
        start_finder(&thread_sx_finder);
    });
    
    let thread_sx_receiver = sx.clone();
    thread::spawn(move || {
        start_receiver(&thread_sx_receiver);
    });

    loop {
        match rx.recv() {
            Ok(f) => {
                keeper.func_handle(f).unwrap();
            },
            Err(e) => { println!("{:?}", e) },
        }
    }
}